#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 导入CSV安装包
'''
csv读写
将二维数组写入csv
读取每一行，读取特定行 特定列
'''
import csv


def csv_write(filename, data):
    '''
    将二维数组写入csv
    '''
    if not (filename and data):
        return False

    # 1. 创建文件对象
    # f = open('文件名.csv','w',encoding='utf-8')
    # 或者：
    try:

        with open(filename, 'w', encoding='utf-8', newline='') as f:
            # 2. 基于文件对象构建 csv写入对象
            csv_writer = csv.writer(f)
        # 3. 构建列表头
            # csv_writer.writerow(["姓名","年,龄","性别"])
        # 4. 写入csv文件内容
            # csv_writer.writerow(["l",'18','男'])

            for row in data:
                csv_writer.writerow(row)
        return True
    except:
        return False


# 5. 关闭文件
# f.close()  # with方法不需要
if __name__ == '__main__':
    # csv写入将二维数组写入csv
    # data = [
    #     ['结果', 'sentence', 'answer', 'expected_answer'],
    #     ['pass', '西游记作者', '《西游记》在欧美很受欢迎。', '西游记是经典，不能重新编写哦。,《西游记》在欧美很受欢迎。'],
    #     ['fail', '今天星期几', '今天星期4，今天是2022年4月21日', '西游记是经典，不能重新编写哦。'],
    #     ['pass', '陋室铭', '山不在高，有仙则名。水不在深，有龙则灵。斯是陋室，惟吾德馨。苔痕上阶绿，草色入帘青。谈笑有鸿儒，往来无白丁。可以调素琴，阅金经。无丝竹之乱耳，无案牍之劳形。南阳诸葛庐，西蜀子云亭。孔子云：何陋之有？', '']]
    # filename = 'r.csv'
    # csv_write(filename,data)

    with open('bb211222b0.csv', 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        print(type(reader))
    # 获取每一行

        for row in reader:
            print(row)
            print(type(row))


#     with open('data.csv', 'r') as f:
#         reader = csv.reader(f)
#         print(type(reader))
# ### 获取某一行
#         result = list(reader)
#         print(result[1])


#     with open('data.csv', 'r') as f:
#         reader = csv.reader(f)
# # 获取某一列
#         column = []
#         for i in reader:
#             column.append(i[4])
#         print(column)
